package com.yidu.hbatsft.action;


import com.yidu.hbatsft.domain.AtsDataDic;
import com.yidu.hbatsft.service.AtsDataDicService;
import com.yidu.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hp
 */
@MultipartConfig
@Controller
@RequestMapping("/dicAction")
public class AtsDataDicController {

    @Resource
    AtsDataDicService dicSerivce;

    /**
     * 查询所有
     * @param dname
     * @param dtype
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectAll")
    public Map<String,Object> selectAll(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,String dname, String dtype,String dcode){
        Map<String,Object> map = new HashMap<>();
        int count = dicSerivce.count(dname,dtype,dcode);
        List<AtsDataDic> list = dicSerivce.selectAll(dname,dtype,dcode,(page-1)*limit,limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);
        return map;
    }

    /**
     * 修改状态
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateId")
    public int updateId(String id){
        return dicSerivce.updateId(Integer.valueOf(id));
    }

    /**
     * 根据Id查询
     * @param did
     * @return
     */
    @ResponseBody
    @RequestMapping("/findById")
    public AtsDataDic findById(Integer did){
        AtsDataDic ddic = dicSerivce.findById(did);
        return ddic;
    }

    /**
     * 增加与修改
     * @param atsDataDic
     * @return
     */
    @ResponseBody
    @RequestMapping("/addAndUpdate")
    public Message addAndUpdate(HttpSession session, AtsDataDic atsDataDic){
        int rows = 0;
        //获取session中登录得用户
        //ManagerModel us = (ManagerModel) session.getAttribute("manager");
        Message msg = new Message();
        if(atsDataDic.getDid()==null){
            //获取创建人名称
            //atsDataDic.setCreater(us.getManUser());
            rows = dicSerivce.insertSelective(atsDataDic);
        }else if(atsDataDic.getDid()!=null&&atsDataDic.getDid()>0){
            rows = dicSerivce.updateByPrimaryKeySelective(atsDataDic);
        }
        if (rows>0){
            msg.setNum(1);
            msg.setMsg("操作成功");
        }else{
            msg.setNum(0);
            msg.setMsg("操作失败");
        }
        return msg;
    }

    /**
     * 查询字典类型
     * @param dcode 类型
     * @return 类型集合
     */
    @ResponseBody
    @RequestMapping("selectType")
    public List<AtsDataDic> selectDic(String dcode){
        return dicSerivce.selectDcode(dcode);
    }

}
